package myweb;

import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import com.iflytek.cloud.speech.RecognizerListener;
import com.iflytek.cloud.speech.RecognizerResult;
import com.iflytek.cloud.speech.SpeechConstant;
import com.iflytek.cloud.speech.SpeechError;
import com.iflytek.cloud.speech.SpeechRecognizer;
import com.iflytek.cloud.speech.SpeechUtility;


/**
 * Servlet implementation class MSCServlet
 */

public class MSCServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private	String result=null; 
	   
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MSCServlet() {
    	
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//录音文件的路径
		String fullFileName = getServletContext().getRealPath("./PCM/test.pcm");
		
		//开启语音识别
		SpeechUtility.createUtility( SpeechConstant.APPID +"=5d38863a");
		SpeechRecognizer speechrecognizer=SpeechRecognizer.createRecognizer();
		speechrecognizer.setParameter(SpeechConstant.AUDIO_SOURCE, "-1");
		speechrecognizer.setParameter(SpeechConstant.RESULT_TYPE, "plain");
		speechrecognizer.startListening(recognizerListener);
		
		final byte[] buff = new byte[64*1024];
		FileInputStream fis = new FileInputStream(fullFileName);
		//判断音频文件是否为空
		if(fis.available()==0)
		{
			speechrecognizer.cancel();
		}
		else {
			int lenread=buff.length;
			while(buff.length==lenread)
			{
				lenread = fis.read( buff );
				//把音频数据写入sdk
				speechrecognizer.writeAudio( buff, 0, lenread );
			}
			speechrecognizer.stopListening();
		}
	}
	/**
	 * 听写监听器
	 */
	private RecognizerListener recognizerListener = new RecognizerListener() {

		@Override
		public void onBeginOfSpeech() {
			System.out.println("onBeginOfSpeech enter" );
		}

		@Override
		public void onEndOfSpeech() {
			System.out.println( "onEndOfSpeech enter" );
		}

		/**
		 * 获取听写结果. 获取RecognizerResult类型的识别结果，并对结果进行累加，显示到Area里
		 */
		@Override
		public void onResult(RecognizerResult results, boolean islast) {
			System.out.println( "onResult enter" );
			
			//如果要解析json结果，请考本项目示例的 com.iflytek.util.JsonParser类
//			String text = JsonParser.parseIatResult(results.getResultString());
			String text = results.getResultString();

			if( null!=text ){
				result = result + text;
				System.out.println( text );
			}

			if( islast ){
				System.out.println( "result end" );
			}
		}
		//检测到音量改变
		@Override
		public void onVolumeChanged(int volume) {
			
			if (volume == 0)
				volume = 1;
			else if (volume >= 6)
				volume = 6;
			
		}
		//错误回调函数
		@Override
		public void onError(SpeechError error) {
			System.out.println( "onError enter" );
			
			if (null != error){
				System.out.println("onError Code：" + error.getErrorCode());
				
			}
		}
		//事件回调函数
		@Override
		public void onEvent(int eventType, int arg1, int agr2, String msg) {
			System.out.println( "onEvent enter" );
			//以下代码用于调试，如果出现问题可以将sid提供给讯飞开发者，用于问题定位排查
			/*if(eventType == SpeechEvent.EVENT_SESSION_ID) {
				DebugLog.Log("sid=="+msg);
			}*/
		}
	};
}
